package com.e3.sysmg.security.client;

import java.util.List;

import com.e3.sysmg.security.model.ModuleModel;
import com.e3.web.core.model.ModelHelper;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.MultiField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.google.gwt.user.client.Element;

public class RoleModuleGridField extends MultiField<ModuleModel> {
	
	private static RoleMessages MESSAGES = RoleMessages.INSTANCE;

	protected AdapterField gridAdapter;
	protected AdapterField buttonAdapter;
	protected VerticalPanel buttonBar;

	private Grid<ModuleModel> moduleGridPanel;
	private GridSelectionModel<ModuleModel> gridSelectionModel = new GridSelectionModel<ModuleModel>();
	private ListStore<ModuleModel> moduleGridStore = new ListStore<ModuleModel>();

	private Button addModuleBtn = new MyButton(RoleStateMachine.ADD_MODULE_ACTION);
	private Button removeModuleBtn = new MyButton(RoleStateMachine.REMOVE_MODULE_ACTION);

	private RoleModuleQueryDialog moduleQueryDialog;
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}

	};
	
	public RoleModuleGridField() {
		
		setSize(200, 125);

		gridAdapter = new AdapterField(getRoleModuleGridPanel());
		
		buttonBar = new VerticalPanel();
		buttonBar.setSpacing(5);
		buttonBar.setStyleAttribute("margin", "3px");
		buttonBar.setHorizontalAlign(HorizontalAlignment.CENTER);
		buttonAdapter = new AdapterField(buttonBar);

		add(gridAdapter);
		add(buttonAdapter);
	}

	private Grid<ModuleModel> getRoleModuleGridPanel() {
		if (moduleGridPanel == null) {
			ModelHelper<ModuleModel> mmhelper = new ModelHelper<ModuleModel>();
			List<ColumnConfig> configs = ModuleHelper.INSTANCE.getModuleGridColumnConfigs();
			ColumnModel cm = new ColumnModel(configs);

			moduleGridStore.setModelComparer(mmhelper.getModelComparer());
			moduleGridStore.setKeyProvider(mmhelper.getModelKeyProvider());
			moduleGridPanel = new Grid<ModuleModel>(moduleGridStore, cm);
			moduleGridPanel.setSelectionModel(gridSelectionModel);

		}
		return moduleGridPanel;
	}

	protected void initButtons() {
		addModuleBtn.addSelectionListener(btnSelectListener);
		removeModuleBtn.addSelectionListener(btnSelectListener);
		buttonBar.add(addModuleBtn);
		buttonBar.add(removeModuleBtn);
	}

	@Override
	protected void onRender(Element target, int index) {
		initButtons();
		
		super.onRender(target, index);

	}

	@Override
	protected void onResize(int width, int height) {
		super.onResize(width, height);
		if (orientation == Orientation.HORIZONTAL) {
			int w = (width - buttonAdapter.el().getParent().getWidth());
			w -= (fields.size() * spacing);
			 getRoleModuleGridPanel().setSize(w, height);
		} else {
			for (Field<?> f : fields) {
				f.setWidth(width);
			}
		}
	}
	
	private RoleModuleQueryDialog getModuleQueryDialog(){
		if(moduleQueryDialog == null){
			moduleQueryDialog = new RoleModuleQueryDialog();
			
		}
		return moduleQueryDialog;
	}

	private void onBtnMnuEvent(BaseAction action) {
		if(action == RoleStateMachine.ADD_MODULE_ACTION){
			onAddModuleAction();
		}
		else if(action == RoleStateMachine.REMOVE_MODULE_ACTION){
			onRemoveModuleAction();
		}
	}

	private void onRemoveModuleAction(){
		List<ModuleModel> modules = getRoleModuleGridPanel().getSelectionModel().getSelectedItems();
		if(modules == null || modules.isEmpty()){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_alert_remove_module(), null);
			return;
		}
		for (ModuleModel moduleModel : modules) {
			moduleGridStore.remove(moduleModel);
		}
	}

	public void setAuthedModules(List<ModuleModel> moduleModels){
		moduleGridStore.add(moduleModels);
	}

	private void onAddModuleAction(){
		getModuleQueryDialog().setVisible(true);
	}
	
	public class RoleModuleQueryDialog extends ModuleRefDialog{

		@Override
		protected void onOk() {
			List<ModuleModel> modules = this.getSelectedModels();
			setAuthedModules(modules);
			this.setVisible(false);
		}
		
	}

	public List<ModuleModel> getAuthedModules(){
		return moduleGridStore.getModels();
	}

	@Override
	public void clear() {
		super.clear();
		moduleGridStore.removeAll();
	}

	
}
